package tool

import (
	"quipus/internal/llm"

	mark3labs "github.com/mark3labs/mcp-go/mcp"
)

const (
	LOCAL = "local"
	MCP   = "mcp"
)

type LLMTool interface {
	GetName() string
	GetType() string
	GetToolSchema() *mark3labs.Tool
	Execute(map[string]interface{}) error
}

type ToolPool struct {
	ToolMap map[string]LLMTool
}

func NewToolPool() *ToolPool {
	return &ToolPool{
		ToolMap: make(map[string]LLMTool),
	}
}

func (tp *ToolPool) AddTool(t LLMTool) {
	tp.ToolMap[t.GetName()] = t
}

func (tp *ToolPool) GetTools() []mark3labs.Tool {
	tools := []mark3labs.Tool{}
	for _, t := range tp.ToolMap {
		tools = append(tools, *t.GetToolSchema())
	}
	return tools
}

func (tp *ToolPool) GetTool(name string) LLMTool {
	if tp.ToolMap[name] == nil {
		return nil
	}

	t, ok := tp.ToolMap[name]
	if !ok {
		return nil
	}
	return t
}

func (tp *ToolPool) Execute(toolCall []llm.ToolCall) {
	for _, t := range toolCall {
		tool := tp.GetTool(t.GetName())
		if tool != nil {
			tool.Execute(t.GetArguments())
		}
	}
}
